<?php


namespace addons\shopro\controller;


use think\Db;

class Info extends Base
{
    protected $noNeedLogin = ['qrcode','store'];
    protected $noNeedRight = ['*'];

    public function store()
    {
        $store_id = input('store_id');
        $store = \addons\shopro\model\Store::get($store_id);
        //print_R($store->toArray());die();
        $data = [
            'name' => $store?$store->name:'',
            'img' => $store?$store->image_first:'',
        ];
        $this->success('成功',$data);
    }

    public function payqrcode()
    {

        $id = input('store_id');
        $amount = input('amount');

        if(empty($id)){
             $this->error('store_id必传');
        }
        // 小程序参数
        $appId = 'wx40ca236c146a8e4f'; // 替换为您的小程序 App ID
        $secret = 'cdb3fca214b2da1f9747418365e05aea'; // 替换为您的小程序 App Secret
        //获取access_token
        $accessToken = getAccessToken($appId, $secret);
        $store = \addons\shopro\model\Store::get($id);

        // 生成二维码
        $scene =  'store_id='.$id.'&amount='.$amount; // 替换为您的小程序页面路径
        $width = 600; // 二维码宽度
        $qrcode = generateQrCode($accessToken, $scene, $width);

        // 保存二维码图片
        $filename = time().'.png'; // 保存的文件名

        $directoryPath = 'uploads/pay/'.date('ymd');
        if (!is_dir($directoryPath)) {
            // 目录不存在，创建目录
            mkdir($directoryPath, 0777, true);
        }

        $path = $directoryPath.'/'.$filename;
        file_put_contents($path, $qrcode);

        $qrCodeImage = ROOT_PATH.'public/'.$path;
        // 示例用法
        //$qrCodeImage = $qrCodeImage; // 替换为实际的二维码图路径
        $logoImage = ROOT_PATH.'public/logo.png'; // 替换为实际的Logo图路径
        $imgname = time();
        $outputImagePath = './uploads/shopqr/payqr'.$imgname.'.png'; // 替换为输出带Logo的二维码路径

        $res = generateQRCodeWithLogo($qrCodeImage, $logoImage, $outputImagePath);
        $path = '/uploads/shopqr/payqr'.$imgname.'.png';
        $data = [
            'url' => request()->domain().'/'.$path,
            'name' => $store?$store['name']:'',
        ];
        $this->success('成功',$data);
    }

    public function qrcode()
    {
        $id = input('store_id');
        // 小程序参数
        $appId = 'wx40ca236c146a8e4f'; // 替换为您的小程序 App ID
        $secret = 'cdb3fca214b2da1f9747418365e05aea'; // 替换为您的小程序 App Secret
        $user = $this->auth->getUserinfo();
        //获取access_token
        $accessToken = getAccessToken($appId, $secret);

        if(empty($id)){
            $this->error('store_id必传');
        }
        $store = \addons\shopro\model\Store::get($id);
        // 生成二维码
        $scene = 'store_id='.$id; // 替换为您的小程序页面路径
        $width = 600; // 二维码宽度
        $qrcode = generateQrCode($accessToken, $scene, $width);

        //$user = $this->auth->getUser();
        //$user['id'] = 1;
        // 保存二维码图片
        $filename = 'store'.$id.'.png'; // 保存的文件名

        $directoryPath = 'uploads/shopqr';
        $path = $directoryPath.'/'.$filename;
        if(!file_exists($path)){
            if (!is_dir($directoryPath)) {
                // 目录不存在，创建目录
                mkdir($directoryPath, 0777, true);
            }
            file_put_contents($path, $qrcode);
        }

        $qrCodeImage = ROOT_PATH.'public/'.$path;
        // 示例用法
        //$qrCodeImage = $qrCodeImage; // 替换为实际的二维码图路径
        $logoImage = ROOT_PATH.'public/logo.png'; // 替换为实际的Logo图路径
        $outputImagePath = './uploads/shopqr/qrcode_logo'.$id.'.png'; // 替换为输出带Logo的二维码路径

       $res = generateQRCodeWithLogo($qrCodeImage, $logoImage, $outputImagePath);
       //echo $res;
        //die();

        //echo $path.PHP_EOL;
        $backgroundImage = ROOT_PATH.'public/bg.png';
        $qrCodeImage = ROOT_PATH.'public/uploads/shopqr/qrcode_logo'.$id.'.png';
        $title = $store?$store['name']:'';

        $outputDir = ROOT_PATH.'public/uploads/poster/';
        $outputImagePath = $outputDir.$user['id'].'.png';
        $return_path = 'uploads/poster/'.$user['id'].'.png';
        if(!file_exists($outputImagePath)){
            if (!is_dir($outputDir)) {
                // 目录不存在，创建目录
                mkdir($outputDir, 0777, true);
            }

        }
        $poster = generatePoster($backgroundImage, $qrCodeImage, $title, $outputImagePath);
        //echo $poster;die();


        $data = [
            'url' => request()->domain().'/'.$return_path,
            'name' => $store?$store['name']:'',
        ];
        $this->success('成功',$data);
    }

    public function createPay()
    {
        $total_fee = input('total_fee');
        $store_id = input('store_id');

        if(empty($store_id)){
            $this->error('store_id必传');
        }
        $user = $this->auth->getUser();
        $oauth = Db::table('fa_shopro_user_oauth')->where('user_id',$user['id'])->find();
        if(empty($oauth['openid'])){
             $this->error('授权失败');
        }
        $order_sn = getSn($user['id']);
        $insert = [
            'order_sn' => $order_sn,
            'user_id' => $user['id'],
            'goods_amount' => $total_fee,
            'status' => 0,
            'is_qrorder' =>1,
            'remark' => input('remark'),
            'total_amount' => $total_fee,
            'total_fee' =>$total_fee,
            'pay_fee' => $total_fee,
            'pay_type' => 'wechat',
            'platform' => 'wxMiniProgram',
            'createtime' => time(),
            'updatetime' => time(),
        ];
        $order_id = Db::table('fa_shopro_order')->insertGetId($insert);

        $goods = Db::table('fa_shopro_goods')->find(5);
        $sku = Db::table('fa_shopro_goods_sku_price')->where('goods_id',$goods['id'])->find();

        $insert = [
            'user_id' => $user['id'],
            'order_id' => $order_id,
            'goods_id' => $goods?$goods['id']:0,
            'goods_sku_price_id' => $sku?$sku['id']:0,
            'goods_type' => 'virtual',
            'goods_title' =>$goods?$goods['title']:'',
            'goods_image' => $goods?request()->domain().'/'.$goods['image']:'',
            'goods_price' => $total_fee,
            'goods_original_price' => $total_fee,
            'goods_num' => 1,
            'pay_price' => $total_fee,
            'store_id' => $store_id,
            'dispatch_fee' => 0,
            'dispatch_type' => 'autosend',
            'dispatch_id' => '4',
            'aftersale_status' => 0,
            'comment_status' => 0,
            'refund_status' => 0,
            'createtime' => time(),
            'updatetime' => time(),
        ];
        $row = Db::table('fa_shopro_order_item')->insert($insert);

        $platform = 'wxMiniProgram';
        $payment = 'wechat';
        $notify_url = $this->request->root(true) . '/addons/shopro/pay/notifyx/payment/' . $payment . '/platform/' . $platform;
        $order_data = [
            'order_id' => $order_id,
            'out_trade_no' => $order_sn,
            'total_fee' => $total_fee,
            'openid' => $oauth["openid"],
            'body' => '商城订单支付',
        ];
        $pay = new \addons\shopro\library\PayService($payment, $platform, $notify_url);
        $payParams = $pay->create($order_data);
        $payParams['order_sn'] = $order_sn;
        $payParams['order_id'] = $order_id;
        $this->success('成功',$payParams);
    }

}























